package com.fmob.client.app.activity;


import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import androidx.annotation.NonNull;

import com.tencent.smtt.sdk.QbSdk;
import androidx.startup.Initializer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import cat.ereza.customactivityoncrash.CustomActivityOnCrash;
import sdk.webview.fmc.com.fmcsdk.manager.FMSdk;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.PreferencesHelper;
import sdk.webview.fmc.com.fmcsdk.util.Utility;
import sdk.webview.fmc.com.fmcsdk.util.XLog;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.RetrofitFactory;

public class AppAutoInitializer implements Initializer<Void> {
  private static AppAutoInitializer autoInitializer;

  public static AppAutoInitializer getInstance() {
    if (autoInitializer == null) {
      synchronized (AppAutoInitializer.class) {
        if (autoInitializer == null) {
          autoInitializer = new AppAutoInitializer();
        }
      }
    }
    return autoInitializer;
  }
  SharedPreferences.Editor editor;
  public SharedPreferences sharedPrefs;
  @NonNull
  @Override
  public Void create(@NonNull Context context) {
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    editor = sharedPrefs.edit();
    Application application = (Application) context.getApplicationContext();
    boolean firstOpenAgress = sharedPrefs.getBoolean(Constant.FIRST_OPEN_AGREE, false);
//    if (firstOpenAgress) {
//      initNeedPermissionsSDK(context);
//    }
    return null;
  }

  @NonNull
  @Override
  public List<Class<? extends Initializer<?>>> dependencies() {
    return new ArrayList<>();
  }

  public void initNeedPermissionsSDK(Context context) {
    try {
      String appName = (String) Utility.getMetaData(context, Constant.APP_NAME);
      //获取本地打包的时间戳
      String version_postfix = (String) Utility.getMetaData(context, Constant.FMC_VERSION_TIME);
      String[] versionStr = version_postfix.split("_");
      String fmc_default_address = (String) Utility.getMetaData(context, Constant.FMC_DEFAULT_ADDRESS);
      //自己标准版使用的基本配置
      FMSdk.standard(true, appName, versionStr[1], fmc_default_address, context);
      XLog.i("version_postfix:" + version_postfix);

      PreferencesHelper.init(context); //初始化SharedPreferences工具类
      RetrofitFactory.init("https://3w395264x2.zicp.vip/", context); //初始化Retrofit接口工厂
//        //崩溃弹出异常界面
      CustomActivityOnCrash.install(context);
      CustomActivityOnCrash.setShowErrorDetails(true);

      XLog.i("@@" + (String) Utility.getMetaData(context, "XIAOMI_APPKEY"));


    } catch (Exception e) {
      e.printStackTrace();
    }
  }












}
